<?php

function clean_the_hoc($dbh, $limit = 100000, $countrylimit = 10000) {
	ini_set("memory_limit","100M");
	$sorts = array("coolness", "num");
	$userids = array();

	//select all users
	if (!$query = mysql_query("SELECT userid FROM flags_updated", $dbh)) die (mysql_error());

	//these are all subject to deletion
	while($row = mysql_fetch_array($query)) {
		$userids[$row['userid']] = true;
	}
	
	/*
	//users in a fed are unmarked
	if (!$query = mysql_query(
		"SELECT DISTINCT u.userid
		FROM flags_updated u, fed_members f
		WHERE u.userid = f.userid", $dbh
	)) die (mysql_error());

	while ($row = mysql_fetch_array($query)) {
		$userids[$row['userid']] = false;
	}
	*/

	//cache the HoC
	$ranking = array();
	if (!$query = mysql_query("SELECT * FROM flags_ranking", $dbh)) die (mysql_error());
	while ($row = mysql_fetch_array($query)) {
		$ranking['coolness'][$row['home']][$row['userid']] = $row['coolness'];
		$ranking['num'][$row['home']][$row['userid']] = $row['num'];
	}

	//get the top $limit for each category, and unmark them by setting them false
	foreach ($sorts as $sort) {
		for ($i = 0; $i <= 3; $i++) {
			//get the top $limit of all, and unmark them by setting them false
			$query = mysql_query(
				"SELECT userid FROM flags_ranking WHERE home = $i ORDER BY $sort DESC, userid DESC LIMIT $limit", $dbh
			) or die (mysql_error());
			while ($row = mysql_fetch_array($query)) {
				$userids[$row['userid']] = false;
			}
			//get the top $countrylimit for each country, and unmark them
			$query = mysql_query(
				"SELECT leagueid FROM leagues_daily WHERE leagueid != 255", $dbh
			) or die (mysql_error());
			while ($row = mysql_fetch_array($query)) {
				$subquery = mysql_query(
					"SELECT u.userid
					FROM flags_updated u, flags_ranking r
					WHERE u.userid = r.userid
					AND home = $i
					AND u.leagueid = {$row['leagueid']}
					ORDER BY $sort DESC, userid DESC
					LIMIT $countrylimit", $dbh
				) or die (mysql_error());
				while ($subrow = mysql_fetch_array($subquery)) {
					$userids[$subrow['userid']] = false;
				}
			}
			//get the top $countrylimit for each fed, and unmark them
			$query = mysql_query(
				"SELECT id FROM fed_details", $dbh
			) or die (mysql_error());
			while ($row = mysql_fetch_array($query)) {
				$subquery = mysql_query(
					"SELECT f.userid
					FROM fed_members f, flags_ranking r
					WHERE f.userid = r.userid
					AND home = $i
					AND allianceid = {$row['id']}
					ORDER BY $sort DESC, userid DESC
					LIMIT $countrylimit", $dbh
				) or die (mysql_error());
			}
		}
	}

	//print and delete users that are still marked, and count the ones left over
	$numleft = 0;
	
	foreach (array_keys($userids) as $userid) {
		foreach ($sorts as $sort) {
			for ($i = 0; $i <= 3; $i++) {
				if (!isset($ranking["$sort"][$i][$userid])) $ranking["$sort"][$i][$userid] = 0;
			}
		}
		$printstring = $ranking['num'][0][$userid] . "/" . $ranking['num'][1][$userid] . "/" . $ranking['num'][2][$userid] . "/" . $ranking['num'][3][$userid] . " // " . $ranking['coolness'][0][$userid] . "/" . $ranking['coolness'][1][$userid] . "/" . $ranking['coolness'][2][$userid] . "/" . $ranking['coolness'][3][$userid];
		if ($userids[$userid] && $printstring !== "0/0/0/0 // 0/0/0/0") {
			echo "$userid is still not marked! N//C is $printstring.\n";
#			if (!mysql_query("DELETE FROM flags_updated WHERE userid = $userid", $dbh)) die (mysql_error());
			if (!mysql_query("DELETE FROM flags_ranking WHERE userid = $userid", $dbh)) die (mysql_error());
		} else {
			$numleft++;
		}
	}

	//remove any leftover dead teams from the HoC ranking
	if (!$query = mysql_query("
		SELECT DISTINCT r.userid
		FROM flags_ranking r
		LEFT JOIN flags_updated u ON r.userid = u.userid
		WHERE u.userid IS NULL", $dbh
	)) die (mysql_error());

	if (mysql_num_rows($query) > 0) while ($row = mysql_fetch_array($query)) {
		if (!mysql_query("DELETE FROM flags_ranking WHERE userid = {$row['userid']}", $dbh)) die (mysql_error());
	}


	//summary
	echo "with global limit $limit and country limit $countrylimit, $numleft users are left in the HoC.\n";
}
